package com.busi.controller.config;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.busi.model.GameConfig;
import com.busi.model.TigerBonusConf;
import com.busi.util.DateUtil;
import com.busi.util.HttpUtils;
import com.busi.util.PropertyUtil;
import com.busi.util.StringUtil;
import com.google.gson.Gson;
import com.zcurd.controller.BaseController;

/**
 * @author loufanglei
 * description :  积分游戏-配置项
 *
 */
public class GameConfigController extends BaseController {
	
	public void listPage(){
		render("list.html");
	}
	
	public void ratePage(){
		LOG.info("#ratePage() 查询游戏税率配置列表");
		GameConfig config = super.getBean(GameConfig.class, "");
		Gson gson = new Gson();
		String body = gson.toJson(config);
		LOG.info("#ratePage()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.domain", "");
		String url = domain+"/emall/admin/gameConfig/listbypage";
		String[][] headers = {{"Content-Type", "application/json"}};
		String jsonString = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#ratePage() 游戏税率配置列表:"+jsonString);
		Map<String, Object> map = JSON.parseObject(jsonString,new TypeReference<Map<String, Object>>(){});
		List<GameConfig> configs = JSON.parseArray(map.get("configs").toString(), GameConfig.class);  
		for(GameConfig con : configs){
			setAttr(con.getCon_key(),con.getCon_value());
		}
		render("rate.html");
	}
	
	/**
	 * @author loufanglei
	 * 积分游戏-配置项信息查询列表
	 * 
	 */
	public void listData(){
		LOG.info("#listData() 查询积分游戏配置项列表");
		TigerBonusConf tiger = super.getBean(TigerBonusConf.class, "");
		Gson gson = new Gson();
		String body = gson.toJson(tiger);
		LOG.info("#listData()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.domain", "");
		String url = domain+"/emall/admin/gameConfig/tigerListbypage";
		String[][] headers = {{"Content-Type", "application/json"}};
		String jsonString = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#listData() 积分游戏配置项列表:"+jsonString);
		
		if (StringUtil.isBlank(jsonString)){
			renderJson();
		} else {
			renderJson(jsonString);
		}
	}
	
	public void updatePage(){
		Integer id = getParaToInt("id");
		Integer times = getParaToInt("times");
		setAttr("id", id);
		setAttr("times", times);
		render("update.html");
	}
	
	public void saveTimes(){
		TigerBonusConf tiger = super.getBean(TigerBonusConf.class, "");
		Timestamp currentTime = DateUtil.getSysDateTimestamp();
		String user_name = getSessionUser().get("user_name");
		tiger.setModUser(user_name);
		tiger.setModtime(currentTime);
		String body = gson.toJson(tiger);
		LOG.info("#saveTimes() 更新游戏奖励倍数,操作人："+user_name);
		LOG.info("#saveTimes()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.domain", "");
		String url = domain+"/emall/admin/gameConfig/tigerUpdate";
		String[][] headers = {{"Content-Type", "application/json"}};
		String jsonString = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#listData() 积分游戏配置项列表:"+jsonString);
		renderSuccess();
	}
	
	public void saveRate(){
		String tigerFee = getPara("tigerFee");
		String tigerMinPoints = getPara("tigerMinPoints");
		String tigerMaxPoints = getPara("tigerMaxPoints");
		String tigerIncrPoints = getPara("tigerIncrPoints");
		String tigerDayLimit = getPara("tigerDayLimit");
		String user_name = getSessionUser().get("user_name");
		Timestamp currentTime = DateUtil.getSysDateTimestamp();
		List<GameConfig> configs = new ArrayList<GameConfig>();
		GameConfig fee = new GameConfig();
		GameConfig min = new GameConfig();
		GameConfig max = new GameConfig();
		GameConfig incr = new GameConfig();
		GameConfig day = new GameConfig();
		fee.setCon_key("GM001_FEE");
		fee.setCon_value(tigerFee);
		fee.setModUser(user_name);
		fee.setModtime(currentTime);
		
		min.setCon_key("GM001_MIN_POINTS");
		min.setCon_value(tigerMinPoints);
		min.setModUser(user_name);
		min.setModtime(currentTime);
		
		max.setCon_key("GM001_MAX_POINTS");
		max.setCon_value(tigerMaxPoints);
		max.setModUser(user_name);
		max.setModtime(currentTime);
		
		incr.setCon_key("GM001_INCR_POINTS");
		incr.setCon_value(tigerIncrPoints);
		incr.setModUser(user_name);
		incr.setModtime(currentTime);
		
		day.setCon_key("GM001_DAY_LIMIT");
		day.setCon_value(tigerDayLimit);
		day.setModUser(user_name);
		day.setModtime(currentTime);
		
		configs.add(fee);
		configs.add(min);
		configs.add(max);
		configs.add(incr);
		configs.add(day);
		LOG.info("#saveRate() 修改游戏税率配置,操作人："+user_name);
		String body = gson.toJson(configs);
		LOG.info("#saveRate()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.domain", "");
		String url = domain+"/emall/admin/gameConfig/update";
		String[][] headers = {{"Content-Type", "application/json"}};
		String jsonString = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#saveRate()  修改游戏税率配置:"+jsonString);
		
		renderSuccess();
	}
	
}
